package utils;

import java.text.DecimalFormat;
import java.util.HashMap;

/**
 * 
 * @author Brent Kersanske
 *
 */
public class FormatUtilities {
	
	/* How many places to round the doubles too. */
	private static final DecimalFormat DOUBLE_FORMATTER = new DecimalFormat("#.#####");
	
	private static HashMap<Integer, String> monthMap;
	
	static {
		monthMap = new HashMap<Integer, String> ();
		monthMap.put(1, "January");
		monthMap.put(2, "February");
		monthMap.put(3, "March");
		monthMap.put(4, "April");
		monthMap.put(5, "May");
		monthMap.put(6, "June");
		monthMap.put(7, "July");
		monthMap.put(8, "August");
		monthMap.put(9, "September");
		monthMap.put(10, "October");
		monthMap.put(11, "November");
		monthMap.put(12, "December");
	}
	
	public static final String getFormattedDate(String year, String month, String day) {
		return getFormattedMonth(month).concat(" ").concat(day).concat(", ").concat(year);
	}
	
	public static final String getFormattedDate(int year, int month, int day) {
		return getFormattedMonth(month).concat(" ").concat(String.valueOf(day)).concat(", ").concat(String.valueOf(year));
	}
	
	public static final String getFormattedMonth(int month) {
		if(monthMap.containsKey(month)) {
			return monthMap.get(month);
		} else {
			return String.valueOf(month);
		}
	}
	
	public static final String getFormattedMonth(String monthNumber) {
		return getFormattedMonth(Integer.valueOf(monthNumber));
	}
	
	public static String removeSpaces(String s) {
		StringBuilder sBuilder = new StringBuilder(s);
		while(sBuilder.indexOf(" ") != -1) {
			int spaceIndex = sBuilder.indexOf(" ");
			sBuilder.replace(spaceIndex, spaceIndex+1, "_");
		}
		return sBuilder.toString();
	}
	
	/**
	 * Gets a formatted String of the different between two doubles.
	 * @param amountA
	 * @param amountB
	 * @return
	 */
	public static String getFormattedDifference(double amountA, double amountB) {
		String formattedDifferenceString = new String();
		double difference				 = roundDouble(amountA - amountB);
		if(difference > 0) {
			formattedDifferenceString = formattedDifferenceString.concat("+");
		}
		formattedDifferenceString = formattedDifferenceString.concat(String.valueOf(difference));
		return formattedDifferenceString;
	}
	
	/**
	 * 
	 * @param value
	 * @return
	 */
	public static double roundDouble(double value) {
		return Double.valueOf(DOUBLE_FORMATTER.format(value));
	}
	
	/**
	 * 
	 * @param value
	 * @return
	 */
	public static String formatDouble(double value) {
		return DOUBLE_FORMATTER.format(value);
	}
}
